# 2024/4/7 下午8:26 HFL
import time

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from api.logger import logger


class Base:

    def __init__(self):
        self.driver = webdriver.Chrome()

    def get(self, url:str):
        try:
            logger.info(f'访问的URL---{url}')
            return self.driver.get(url),self.driver.maximize_window()
        except Exception as e :
            logger.error(f'访问URL:{url}失败，错误信息：{e}')

    # 着色器，便于定位调试
    def locator_station(self, loc):
        self.driver.execute_script(
            'arguments[0].setAttribute("style", arguments[1])', loc,
            "border:3px solid red"  # 边框，红色
        )

    # 定位单个元素
    def find_element(self, loc, retry=1):
        for time1 in range(retry + 1):
            try:
                start_time = time.time()
                element = WebDriverWait(self.driver, 15, 0.5).until(EC.presence_of_element_located(loc))
                self.locator_station(element)
                end_time = time.time()
                logger.info(f'元素定位方法：{loc[0]},元素的位置：{loc[1]},元素定位成功，耗时：{end_time - start_time, 3}')
                return element
            except Exception as e:
                error_info = f"元素：{loc}定位失败，错误信息：{e}"
                logger.error(error_info)
                if time1 < retry:
                    start_time = time.time()
                    end_time = time.time()
                    logger.info(f'元素：{loc}正在重新定位，当前重试次数：{retry}，耗时：{end_time - start_time, 3}')
                else:
                    raise Exception(error_info)

    def find_elements(self, loc, num, retry=1):
        for time1 in range(retry + 1):
            try:
                start_time = time.time()
                elements = WebDriverWait(self.driver, 15, 0.5).until(EC.presence_of_all_elements_located(loc))
                self.locator_station(elements)
                end_time = time.time()
                logger.info(f"元素定位方法：{loc[0]},元素位置：{loc[1]},元素定位成功，耗时：{end_time - start_time, 3}")
                return elements[num]
            except Exception as e:
                error_info = f'元素：{loc}定位失败，错误信息：{e}'
                logger.error(error_info)
                if time1 < retry:
                    start_time = time.time()
                    end_time = time.time()
                    logger.info(f'元素：{loc}正在重新定位，当前重试次数：{retry},耗时：{end_time - start_time, 3}')
                else:
                    raise Exception(error_info)

    def click_element(self, loc):

        try:
            return self.find_element(loc).click(), logger.info(f'元素：{loc} 点击成功')
        except Exception as e:
            error_info = f'元素：{loc}点击失败，错误信息：{e}'
            logger.error(error_info)


    def click_elements(self, loc, num):

        try:
            return self.find_elements(loc, num).click(), logger.info(f"元素：{loc} 点击成功")
        except Exception as e:
            error_info = f"元素：{loc},点击失败,错误信息：{e}"
            logger.error(error_info)


    def switch_window(self):
        try:
            win = self.driver.window_handles
            self.driver.switch_to.window(win[-1])
            logger.info(f"切换新窗口成功")
        except Exception as e:
            error_info = f'切换窗口失败，错误信息:{e}'
            logger.error(error_info)

    def input_text(self, loc, text):

        try:
            ele = self.find_element(loc)
            ele.clear()
            ele.send_keys(text)
            logger.info(f'成功输入：{text}')
        except Exception as e:
            error_info = f'文本{text}输入失败,错误信息：{e}'
            logger.error(error_info)


    def switch_to_iframe(self, reference=None, retry=1):
        for time1 in range(retry + 1):
            try:
                logger.info(f"iframe:{reference}切换成功")
                if not reference:
                    return self.driver.switch_to.default_content()
                self.driver.refresh()
                return WebDriverWait(self.driver, 10, 1).until(EC.frame_to_be_available_and_switch_to_it(reference))
            except Exception as e:
                error_info = f'iframe：{reference}切换失败，错误信息：{e}'
                logger.error(error_info)
                if time1 < retry:
                    logger.info(f'iframe正在重新切换，当前重试次数：{retry}')
                else:
                    raise Exception(error_info)

    def refresh(self):
        try:
            logger.info('刷新页面成功')
            self.driver.refresh()
        except Exception as e:
            logger.error(f'页面刷新失败，错误信息：{e}')
            # raise Exception(f'页面刷新失败，错误信息：{e}')
